<template>
  <div>
    <div class="flex justify-between items-center border-b border-color-gray-300">
      <div>通知</div>
      <div><a-button type="link" @click="fnClearMsg">全部清空</a-button></div>
    </div>
    <List
      :class="prefixCls"
      bordered
      :pagination="getPagination"
      style="width: 350px; border: none"
    >
      <template v-for="item in getData" :key="item.id">
        <List.Item class="list-item">
          <List.Item.Meta>
            <template #description>
              <div>
                <!--                <div class="title">{{ item.type_name }}</div>-->
                <div class="desc hover:text-blue-500" @click="onTitleClick && onTitleClick(item)">{{
                  item.tips_desc
                }}</div>
                <div class="time flex justify-between">
                  <span>{{ item.datetime }}</span>
                  <span>
                    <a-tag :color="getTagColor(item.state_id)">{{ item.state_name }}</a-tag>
                  </span>
                </div>
              </div>
            </template>
          </List.Item.Meta>
        </List.Item>
      </template>
    </List>
  </div>
</template>
<script lang="ts" setup>
  import { computed, PropType, ref, watch, unref } from 'vue';
  import { List, Tag } from 'ant-design-vue';
  import { useDesign } from '@/hooks/web/useDesign';
  import { isNumber } from '@/utils/is';
  import { INotification } from '@/api/system/model/systemModel';

  const props = defineProps({
    list: {
      type: Array as PropType<INotification[]>,
      default: () => [],
    },
    pageSize: {
      type: [Boolean, Number] as PropType<Boolean | Number>,
      default: 5,
    },
    currentPage: {
      type: Number,
      default: 1,
    },
    titleRows: {
      type: Number,
      default: 1,
    },
    descRows: {
      type: Number,
      default: 2,
    },
    onTitleClick: {
      type: Function as PropType<(Recordable) => void>,
    },
  });

  const emit = defineEmits(['update:currentPage', 'update:clearMsg']);
  const ATag = Tag;

  const { prefixCls } = useDesign('header-notify-list');
  const current = ref(props.currentPage || 1);
  const getData = computed(() => {
    const { pageSize, list } = props;
    if (pageSize === false) return [];
    let size = isNumber(pageSize) ? pageSize : 5;
    return list.slice(size * (unref(current) - 1), size * unref(current));
  });
  watch(
    () => props.currentPage,
    (v) => {
      current.value = v;
    },
  );
  const getPagination = computed(() => {
    const { list, pageSize } = props;
    console.log(pageSize);
    // compatible line 104
    // if typeof pageSize is boolean, Number(true) && 5 = 5, Number(false) && 5 = 0
    const size = isNumber(pageSize) ? pageSize : Number(pageSize) && 5;

    if (size > 0 && list && list.length > size) {
      return {
        total: list.length,
        pageSize: size,
        showLessItems: true,
        size: 'small',
        current: unref(current),
        onChange(page) {
          current.value = page;
          emit('update:currentPage', page);
        },
      };
    } else {
      return false;
    }
  });
  const getTagColor = (stateId: number) => {
    switch (stateId) {
      case 2:
        return 'success';
      case 1:
        return 'pink';
      default:
        return ''; // 默认颜色或其他颜色
    }
  };

  function fnClearMsg() {
    emit('update:clearMsg', 1);
  }
</script>
<style lang="less" scoped>
  @prefix-cls: ~'@{namespace}-header-notify-list';

  .@{prefix-cls} {
    &::-webkit-scrollbar {
      display: none;
    }

    ::v-deep(.ant-pagination-disabled) {
      display: inline-block !important;
    }

    .list-item {
      padding: 6px;
      overflow: hidden;
      transition: all 0.3s;
      cursor: pointer;

      .title {
        margin-bottom: 8px;
        font-weight: normal;

        .extra {
          margin-top: -1.5px;
          margin-right: 0;
          float: right;
          font-weight: normal;

          .tag {
            margin-right: 0;
          }
        }
      }

      .avatar {
        margin-top: 4px;
      }

      .description {
        font-size: 12px;
        line-height: 18px;
      }

      .datetime {
        margin-top: 4px;
        font-size: 12px;
        line-height: 18px;
      }
    }
  }
</style>
